package 实验三.二层cs;

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class SimpleClient {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        String userInput = null;

        try {
            socket = new Socket("localhost", 12345);
            System.out.println("已连接到服务器");
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);

            while (true) {
                System.out.println("1:添加联系人 2:修改联系人 3:删除联系人 4:查看联系人 5:退出");
                System.out.println("请输入发送到服务器的消息（输入序号）：");
                userInput = sc.nextLine();


                switch (userInput) {
                    case "1":
                        Contact contact = add();
                        out.println("1-" + contact.name + "-" + contact.address + "-" + contact.phone); // 发送添加联系人信息到服务器
                        //接收服务器的响应
                        String response = in.readLine();
                        System.out.println("服务器响应：" + response);
                        break;
                    case "2":
                        String nameToModify = modify();
                        System.out.println("请输入修改后的地址和电话（用-分隔）：");
                        String modifiedInfo = sc.nextLine();
                        //将字符串分割
                        String[] modifiedInfoArray = modifiedInfo.split("-");
                        out.println("2-" + nameToModify + "-" + modifiedInfoArray[0] + "-" + modifiedInfoArray[1]); // 发送修改联系人信息到服务器
                        //接收服务器的响应
                        String response2 = in.readLine();
                        System.out.println("服务器响应：" + response2);
                        break;
                    case "3":
                        String nameToDelete = delete();
                        out.println("3-" + nameToDelete); // 发送删除联系人信息到服务器
                        //接收服务器的响应
                        String response3 = in.readLine();
                        System.out.println("服务器响应：" + response3);
                        break;
                    case "4":
                        out.println("4"); // 发送查看联系人请求到服务器
                        String contacts = in.readLine(); // 接收服务器返回的联系人数组
                        System.out.println("服务器返回的联系人信息：" + contacts);
                        break;
                    case "5":
                        System.out.println("退出");
                        socket.close(); // 关闭连接
                        return;
                    default:
                        System.out.println("输入错误");
                        break;
                }



            }
        } catch (UnknownHostException e) {
            System.err.println("无法找到服务器");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
                if (out != null) out.close();
                if (socket != null && !socket.isClosed()) socket.close(); // 确保连接已关闭
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static Contact add() {
        System.out.println("请输入:姓名-地址-电话");
        String[] info = sc.nextLine().split("-");

        String name = info[0];
        String address = info[1];
        String phone = info[2];
        return new Contact(name, address, phone);
    }

    static String modify() {
        System.out.println("请输入要修改的联系人姓名:");
        String name = sc.nextLine();
        return name;
    }

    static String delete() {
        System.out.println("请输入要删除的联系人姓名:");
        String name = sc.nextLine();
        return name;
    }
}